class Solution {
    int res = 0;
    int maxd=0;
    public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) {
        List<Integer>[] list = new ArrayList[n];
        Arrays.setAll(list,e->new ArrayList<>());
        for(int i =0;i<n;i++)
        {
           if(manager[i]>=0)
           {
            list[manager[i]].add(i);
           }    
        }
        int had = dfs(headID,list,informTime);
        return had;
        
    }
    public int dfs(int node,List<Integer>[] list,int[] informTime)
    {
        int res=0;
        
        for(int var:list[node])
        {
            res = Math.max(res,dfs(var,list,informTime));
        }
        return res+informTime[node];
    }
}